package com.bj58.wf.training.first.interceptors;

import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext;
import com.bj58.wf.mvc.interceptor.ActionInterceptor;

/**
 * 实现的一个方法拦截器
 * @author renjun
 *
 */
public class CrazyInterceptor implements ActionInterceptor {

	int count = 0;
	
	/**
	 * 由于不是线程安全，所以需要同步处理
	 * @return
	 */
	private synchronized int getCount(){
		count ++;
		if (count > 4)
			count = 0;
		
		return count;
	}
	
	/**
	 * 判断中奖没？:)
	 */
	public ActionResult preExecute(BeatContext beat) {
		int count = getCount();
		
		return count == 0 ? ActionResult.view("crazy")
				: null;
	}

}
